import { Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { Repository } from 'typeorm';
import { EventLog } from './event-log.entity';
import { CreateEventLogDto  } from './dto/create-event-log.dto';
import {
  toPost200,
  to400,
  to404,
  toGet200,
  toList200,
} from '../tools';

@Injectable()
export class EventLogService {
    constructor(
        @InjectRepository(EventLog)
        private eventLogRepository: Repository<EventLog>,
      ) {}

      // 新增
      async create(data: CreateEventLogDto): Promise<any> {
        const newEventLog = this.eventLogRepository.create(data);
    
        this.eventLogRepository.save(newEventLog);
    
        return toPost200();
      }

      // 分页查
      async findAll(query: any): Promise<any> {
        let { pageNum, pageSize, method, operatorId, sufferOperatorId, type } = query;

        pageSize = Number(pageSize ?? 10);
        pageNum = Number(pageNum ?? 1) ;
        const skip = (pageNum - 1) * pageSize;
        const where: any = {};
        if (method) where.method = method;
        if (operatorId) where.operatorId = operatorId;
        if (sufferOperatorId) where.sufferOperatorId = sufferOperatorId;
        if (type) where.type = type;
        const [data, total] = await this.eventLogRepository.findAndCount({
          skip,
          take: pageSize,
          withDeleted: true,
          where,
          order: {
            createdAt: 'DESC',
          }
          // where: otherParams, // 处理其他查询参数
        });
    
    
        return toList200(total, pageNum, pageSize, data);

      }
}
